/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nhadat.action;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import nhadat.action.form.LoginActionForm;
import nhadat.action.form.PreUpdateActionForm;
import nhadat.common.CommonConstant;
import nhadat.dao.TindangDao;
import nhadat.entity.TblTinDang;
import nhadat.tindang.TindangType;
import nhadat.tindangfull.ChieudaikhuonvienType;
import nhadat.tindangfull.ChieudaixaydungType;
import nhadat.tindangfull.ChieungangkhuonvienType;
import nhadat.tindangfull.ChieungangxaydungType;
import nhadat.tindangfull.DientichkhuonvienfullType;
import nhadat.tindangfull.DientichxaydungfullType;
import nhadat.tindangfull.GiatienfullType;
import nhadat.tindangfull.KhuvucfullType;
import nhadat.tindangfull.TindangfullType;
import nhadat.xml.StaxParser;
import nhadat.xml.XMLmanipulate;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author huanht
 */
public class PreUpdateAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        PreUpdateActionForm f = (PreUpdateActionForm) form;
        String action = request.getParameter("action");

        if (action.equals("Chỉnh Sửa")) {
            TblTinDang tin = f.xemChiTiet();
            HttpSession session = request.getSession(true);
            session.setAttribute("INFOUpdate", tin);
            return mapping.findForward(SUCCESS);
        } else if (action.equals("Xoá Bài")){
            LoginActionForm loginForm = (LoginActionForm) request.getSession().getAttribute("LoginActionForm");
            String username = loginForm.getTendangnhap();
            f.deleteTinDang();
            f.recordForReport(username);
            return mapping.findForward("delete");
        } else if (action.equals("Hoàn Tất")) {
         
            String delimiter = ",";
            String[] tmp;
            tmp = f.getXmlString().split(delimiter);
            for (int i = 0; i < tmp.length; i++) {
                deleteTinDang(tmp[i]);
            }
            
            return mapping.findForward("delete");
        }
        return mapping.findForward(SUCCESS);
    }

    public void deleteTinDang(String msts) {

        XMLmanipulate test = new XMLmanipulate();
        StaxParser parser = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");


            TindangfullType[] dsTindang;

            DientichkhuonvienfullType dientichkhuonvien = new DientichkhuonvienfullType();
            ChieungangkhuonvienType chieungangkhuonvien = new ChieungangkhuonvienType();
            ChieudaikhuonvienType chieudaikhuonvien = new ChieudaikhuonvienType();

            DientichxaydungfullType dientichxaydung = new DientichxaydungfullType();
            ChieungangxaydungType chieungangxaydung = new ChieungangxaydungType();
            ChieudaixaydungType chieudaixaydung = new ChieudaixaydungType();

            KhuvucfullType khuvuc = new KhuvucfullType();
            GiatienfullType giatien = new GiatienfullType();



            List<TindangfullType> listTinDangFullType = parser.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
            dsTindang = new TindangfullType[listTinDangFullType.size() + 1];
            for (int i = 0; i < listTinDangFullType.size(); i++) {
                dsTindang[i] = listTinDangFullType.get(i);
            }


            dientichkhuonvien.setDvddientichkhuonvien(" ");
            dientichkhuonvien.setSolieu(0);
            chieungangkhuonvien.setDvdchieungangkhuonvien(" ");
            chieungangkhuonvien.setSolieu(0);
            chieudaikhuonvien.setDvdchieudaikhuonvien(" ");
            chieudaikhuonvien.setSolieu(0);


            dientichxaydung.setDvddientichxaydung(" ");
            dientichxaydung.setSolieu(0);
            chieungangxaydung.setDvdchieungangxaydung(" ");
            chieungangxaydung.setSolieu(0);
            chieudaixaydung.setDvdchieudaixaydung(" ");
            chieudaixaydung.setSolieu(0);

            khuvuc.setQuanhuyen(" ");
            khuvuc.setTinhthanh(" ");

            giatien.setSolieu(0);
            giatien.setDvdgiatien(" ");

            TindangfullType deletedTin = new TindangfullType();

            deletedTin.setMatindang(msts);
            deletedTin.setTenloaitin(" ");
            deletedTin.setTenloainha(" ");
            deletedTin.setTenloaigiayto(" ");
            deletedTin.setThongtinchitiet(" ");
            deletedTin.setCautruc(" ");
            deletedTin.setDientichkhuonvien(dientichkhuonvien);
            deletedTin.setChieungangkhuonvien(chieungangkhuonvien);
            deletedTin.setChieudaikhuonvien(chieudaikhuonvien);
            deletedTin.setDientichxaydung(dientichxaydung);
            deletedTin.setChieungangxaydung(chieungangxaydung);
            deletedTin.setChieudaixaydung(chieudaixaydung);
            deletedTin.setDiachi(" ");
            deletedTin.setKhuvuc(khuvuc);
            deletedTin.setHinhanh(" ");
            deletedTin.setTieudetin(" ");
            deletedTin.setGiatien(giatien);
            deletedTin.setNguoidangtin(" ");
            Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            String dateNow = formatter.format(currentDate.getTime());
            deletedTin.setNgaydangtin(dateNow);
            deletedTin.setLancuoicapnhat(dateNow);
            deletedTin.setSoluotxem(new BigInteger(0+""));
            deletedTin.setStatus("delete");
            deletedTin.setIsActive("False");

            dsTindang[listTinDangFullType.size()] = deletedTin;

            test.createXMLfromTindangFullType(dsTindang, path + "xml/TindangFull.xml");
            //-----------------------------------------------------------------------------------
            //-----------sửa 1 record trong Tindang.xml thành status = delete---------------------

            TindangType[] dsTinDangType;


            List<TindangType> listTinDangType = parser.readXMLtoTindangTypebyStAX(path + "xml/Tindang.xml");
            dsTinDangType = new TindangType[listTinDangType.size()];
            for (int i = 0; i < listTinDangType.size(); i++) {
                dsTinDangType[i] = listTinDangType.get(i);
            }

            for (int i = 0; i < dsTinDangType.length; i++) {
                if (dsTinDangType[i].getMatindang().equals(msts)) {

                    dsTinDangType[i].setStatus("delete");

                }
            }

            test.createXMLfromTindangType(dsTinDangType, path + "xml/Tindang.xml");

            //-----------------------------------------------------------------------------------


    }
}
